import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.CometEvent;

public class Game {
	String pl1 = null;
	String pl2 = null;
	int id = 0;
	ArrayList<Ship> pl1Ships;
	ArrayList<Ship> pl2Ships;
	
	HttpServletResponse pl1response = null;
	HttpServletResponse pl2response = null;
	boolean turnDecided = false;
	private CometEvent player1Event;
	private CometEvent player2Event;


	public CometEvent getPlayer1Event() {
		return player1Event;
	}

	public void setPlayer1Event(CometEvent player1Event) {
		this.player1Event = player1Event;
	}

	public CometEvent getPlayer2Event() {
		return player2Event;
	}

	public void setPlayer2Event(CometEvent player2Event) {
		this.player2Event = player2Event;
	}

	boolean pl2IsReady = false;
	boolean pl1IsReady = false;

	public boolean isPl2IsReady() {
		return pl2IsReady;
	}

	public void setPl2IsReady(boolean pl2IsReady) {
		this.pl2IsReady = pl2IsReady;
	}

	public boolean isPl1IsReady() {
		return pl1IsReady;
	}

	public void setPl1IsReady(boolean pl1IsReady) {
		this.pl1IsReady = pl1IsReady;
	}

	public boolean isTurn() {
		return turnDecided;
	}

	public void setTurn(boolean turnDecided) {
		this.turnDecided = turnDecided;
	}

	public Game(String pl1, String pl2, int id, ArrayList<Ship> pl1Ships,
			ArrayList<Ship> pl2Ships, HttpServletResponse pl1response,
			HttpServletResponse pl2response, boolean turnDecided,
			boolean pl2IsReady, boolean pl1IsReady) {
		super();
		this.pl1 = pl1;
		this.pl2 = pl2;
		this.id = id;
		this.pl1Ships = pl1Ships;
		this.pl2Ships = pl2Ships;
		this.pl1response = pl1response;
		this.pl2response = pl2response;
		this.turnDecided = turnDecided;
		this.pl2IsReady = pl2IsReady;
		this.pl1IsReady = pl1IsReady;
	}
	
	public String winnerCheck(){
		int player1AliveShipCount = pl1Ships.size();
		int player2AliveShipCount = pl2Ships.size();
		
		for(Ship ship : pl1Ships){
			if(ship.getHealth()==0){
				player1AliveShipCount--;
			}
		}		
		for (Ship ship : pl2Ships){
			if(ship.getHealth()==0){
				player2AliveShipCount--;
			}
		}
		if(player1AliveShipCount==0){
			return pl2;
		}else if(player2AliveShipCount==0){
			return pl1;
		}else{
			return null;
		}		
	}

	public Ship strike(String strikeId, boolean IsPlayerOne) {
		if (IsPlayerOne) {
			for (Ship ship : pl2Ships) {
				for (String shipCoordinate : ship.coordinates) {
					if (shipCoordinate.equals(strikeId) && ship.getHealth() > 0) {
						ship.setHealth(ship.getHealth() - 1);
						return ship;
					}
					
				}
			}
			return null;
		}else{
			
			for (Ship ship : pl1Ships) {
				for (String shipCoordinate : ship.coordinates) {
					if (shipCoordinate.equals(strikeId) && ship.getHealth() > 0) {
						ship.setHealth(ship.getHealth() - 1);
						return ship;
					}
				}
			}
			return null;
			
		}

	}

	public HttpServletResponse getPl1Response() {
		return pl1response;
	}

	public void setPl1Response(HttpServletResponse Pl1Response) {
		this.pl1response = Pl1Response;
	}

	public HttpServletResponse getPl2Response() {
		return pl2response;
	}

	public void setPl2Response(HttpServletResponse Pl2Response) {
		this.pl2response = Pl2Response;
	}

	public ArrayList<Ship> getPl1Ships() {
		return pl1Ships;
	}

	public void setPl1Ships(ArrayList<Ship> pl1Ships) {
		this.pl1Ships = pl1Ships;
	}

	public ArrayList<Ship> getPl2Ships() {
		return pl2Ships;
	}

	public void setPl2Ships(ArrayList<Ship> pl2Ships) {
		this.pl2Ships = pl2Ships;
	}

	public String getPl1() {
		return pl1;
	}

	public void setPl1(String pl1) {
		this.pl1 = pl1;
	}

	public String getPl2() {
		return pl2;
	}

	public void setPl2(String pl2) {
		this.pl2 = pl2;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}
